"""
模拟计算密集型场景，单进程跑一遍
"""
import datetime


def calc():
    sum = 0
    # 1到1亿的加法
    for i in range(100000000):
        sum += 1

    return sum


start = datetime.datetime.now()

# 使用单进程（主线程），跑5遍
calc()
calc()
calc()
calc()
calc()

end = (datetime.datetime.now() - start).total_seconds()
print(end)

"""
模拟计算密集型场景，多进程跑一遍
"""

import datetime
import threading


def calc():
    sum = 0
    # 1到1亿的加法
    for i in range(100000000):
        sum += 1

    return sum


start = datetime.datetime.now()

# 使用多进程同时跑，跑5遍
lst = []

for _ in range(5):
    t = threading.Thread(target=calc)
    t.start()
    lst.append(t)

for t in lst:
    t.join()

end = (datetime.datetime.now() - start).total_seconds()
print(end)
